// lib XML HTTP requirer
function Xhr(uri, params, via, sync) {
	this.setParams = function(p) {
		params = p instanceof Object && !(p instanceof String) ? 
		 	param_encode(p) : p
	}

	params ? this.setParams(params) : params = ""
	via == 'POST' ? via : via = 'GET'
	this.status = NaN
	this.onready = Function()
	this.onfail = Function()
	var self

	if (window.XMLHttpRequest)
		self = new XMLHttpRequest()
	else {
		var atps = ['MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0', 
			'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP']
		while (kw = atps.shift()) {
			try {
				self = new ActiveXObject(kw)
				break
			} catch (e) {
				if (atps == false) throw e
			}
		}
	}

	this.start = function() {
		var xhr = this
		self.onreadystatechange = function () {
			if (self.readyState == 4) {
				xhr.status = self.status
				if (xhr.status == 200) xhr.onready()
				else xhr.onfail()
			}
		}
		self.open(via, uri + 
				(via == 'GET' && params ? "?" + params : ""), !sync)
		if (via == 'POST') {
			self.setRequestHeader("Content-type", 
					"application/x-www-form-urlencoded")
		}
		self.send(via == 'GET' && !params ? null : params)
	}

	this.getText = function() { return self.responseText }
	this.getXML = function() { return self.responseXML }
}

function param_encode(o) {
	var ls = []
	for (var i in o)
		if (o.hasOwnProperty(i)) 
			ls.push(encodeURIComponent(i) + '=' + 
					encodeURIComponent(o[i]))
	return ls.join('&')
}

function as_array(col) {
	return Array.prototype.slice.call(col)
}

function get_cookie(name) {
	var results = document.cookie.match
		('(^|;) ?' + name + '=([^;]*)(;|$)')
	if (results)
		return decodeURIComponent(results[2])
	else return ""
}

// convenient DOM tools
var $ = function() {
	var _mem = {}
	return function(node, id) {
		if (!id) {
			id = node
			node = document
		}
		if (id in _mem)
			return _mem[id]
		return _mem[id] = node.getElementById(id)
	}
}()

var $$ = function(node, tag) {
	if (!tag) {
		tag = node
		node = document
	}
	return node.getElementsByTagName(tag)
}

function $node(o) {
	return typeof o == 'string' || o instanceof String ? 
			document.createTextNode(o) : o
}

function $make(tagName, id, className, innerHTML) {
	var e = document.createElement(tagName)
	if (id && id instanceof Object) {
		for (var i in id)
			e.setAttribute(i, id[i])
		if (className) e.innerHTML = className
	} else {
		if (id) e.id = id
		if (className) e.className = className
		if (innerHTML) e.innerHTML = innerHTML
	}
	return e
}

function $add(node, ls) {
	if (ls instanceof Array)
		for (var i = 0; i < ls.length; i++)
			node.appendChild($node(ls[i]))
	else for (var i = 1; i < arguments.length; i++)
		node.appendChild($node(arguments[i]))
	return node
}

function $insert(node, ls) {
	var first = node.firstChild
	if (ls instanceof Array)
		for (var i = 0; i < ls.length; i++)
			node.insertBefore($node(ls[i]), first)
	else for (var i = 1; i < arguments.length; i++)
		node.insertBefore($node(arguments[i]), first)
	return node
}

function $replace(node, o) {
	node.parentNode.replaceChild($node(o), node)
	return node
}

function $del(ls) {
	if (arguments.length > 1) ls = as_array(arguments)
	if (ls instanceof Array)
		for (var i = 0; i < ls.length; i++)
			ls[i].parentNode.removeChild(ls[i])
	else ls.parentNode.removeChild(ls)
}

function $del_row(child) {
	for (var e = child; e != null; e = e.parentNode) {
		if (e.tagName == 'TR') {
			$del(e)
			return
		}
	}
}
